/*
 * EnemySpriteBaseDirectional.cs
 *
 * Created on January 4, 2008, 1:21 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace HexTD
{
	public abstract class EnemySpriteBaseDirectional : EnemySpriteBase
	{

		private int prevPaintTime;
		private Color bodyColor;
		private string bodySVGName;
		private bool bodyUsesSVG;
		private float bodyScale;
		private int bodyStep = 0;
#if PORT
		private Brush bodyStroke;
		private Shape[] bodyShapes;
		private RotateTransform atRotate, atCorrect;
#endif

		/** Creates a new instance of EnemySpriteBaseDirectional */
		public EnemySpriteBaseDirectional(Game game, string name)
			: base(game, name)
		{
			this.bodyUsesSVG = false;
		}

		public EnemySpriteBaseDirectional(Game game, string name, bool withImage)
			: base(game, name)
		{
			this.bodyUsesSVG = withImage;
		}

		protected override void doInit(Context context, int path, float delay, long health, int price, Size s)
		{
#if PORT
			this.atRotate = new RotateTransform();
			this.atCorrect = new RotateTransform();
#endif
			base.doInit(context, path, delay, health, price, s);
		}

		public override void doTick(int gameTime)
		{
#if PORT
			float oldx = this.getX();
			float oldy = this.getY();
			base.doTick(gameTime);
			double a = Math.Atan2(this.getY() - oldy, this.getX() - oldx);
			this.atRotate.setToIdentity();
			this.atRotate.rotate(a);
#endif
		}

#if PORT
		public abstract Shape[] generateShapes();
#endif

		public abstract Color generateColor();

#if PORT
		public abstract Brush generateStroke();
#endif

		public abstract string getSvgName();

		public override void resetScale()
		{
			base.resetScale();
#if PORT
			if (this.bodyUsesSVG) {
				this.bodySVGName = this.getSvgName();
				this.bodyScale = this.size.scaleMod * this.context.paintScale / 2;
				atCorrect.setToIdentity();
				atCorrect.setToTranslation(-this.bodyScale / 2, -this.bodyScale / 2);
			} else {
				this.bodyColor = this.generateColor();
				this.bodyShapes = this.generateShapes();
				this.bodyStroke = this.generateStroke();
			}
#endif
		}

		public override void Draw(GameTime gameTime)
		{
#if PORT
			if (this.inactive || this.dead)
			{
			} else {
				Brush defaultStroke = g2.getStroke();

				RotateTransform saveXform = g2.getTransform();
				g2.transform(atTranslate);

				if (this.shielded) {
					this.paintShield_bottom(g2);
				}

				this.paintHealthLine(g2);

				g2.transform(atRotate);
				if (this.bodyUsesSVG) {
					g2.transform(this.atCorrect);
					context.renderSvgImage(this.bodySVGName, g2, (int) this.bodyScale, (int) this.bodyScale);
				} else {
					this.bodyStep += gameTime - this.prevPaintTime;
					if (this.bodyStep >= this.bodyShapes.length) {
						this.bodyStep = 0;
					}
					this.prevPaintTime = gameTime;

					g2.setColor(this.bodyColor);
					g2.setStroke(this.bodyStroke);
					g2.draw(this.bodyShapes[this.bodyStep]);
				}
				g2.setTransform(saveXform);

				if (this.shielded) {
					g2.transform(atTranslate);
					this.paintShield_top(g2);
					g2.setTransform(saveXform);
				}

				base.paintSelected(g2);

				g2.setStroke(defaultStroke);
			}
#endif
		}
	}
}